/*
 * Copyright 2010 Alessandro Bellucci
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.soupframework.gwt.designerpanel.client.decorator.impl;

import java.util.HashMap;

import org.soupframework.gwt.designerpanel.client.decorator.ObjectDecorator;

import com.google.gwt.dom.client.Style.BorderStyle;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Widget;

public class BorderWidgetDecorator<T> implements ObjectDecorator<T> {

	private BorderStyle borderStyle = BorderStyle.DASHED;
	private int borderWidth = 1;
	private String borderColor = "#0000ff";

	private HashMap<Widget, WidgetBorderStyle> originalBorderStyle = new HashMap<Widget, WidgetBorderStyle>();

	private static class WidgetBorderStyle {
		private String borderStyle;
		private String borderWidth;
		private String borderColor;

		public WidgetBorderStyle(String borderStyle, String borderWidth,
				String borderColor) {
			super();
			this.borderStyle = borderStyle;
			this.borderWidth = borderWidth;
			this.borderColor = borderColor;
		}

		public String getBorderStyle() {
			return borderStyle;
		}
		public String getBorderWidth() {
			return borderWidth;
		}
		public String getBorderColor() {
			return borderColor;
		}
	}

	public BorderWidgetDecorator() {
	}

	public BorderWidgetDecorator(BorderStyle borderStyle, int borderWidth, String borderColor) {
		super();
		this.borderStyle = borderStyle;
		this.borderWidth = borderWidth;
		this.borderColor = borderColor;
	}

	public BorderStyle getBorderStyle() {
		return borderStyle;
	}

	public int getBorderWidth() {
		return borderWidth;
	}

	public String getBorderColor() {
		return borderColor;
	}

	@Override
	public void decorateObject(T object, Widget objectView) {
		if(objectView != null && objectView.getElement() != null){
			if(originalBorderStyle.get(objectView) != null){
				// Widget already highlighted
			}
			else{
				// save old border style
				WidgetBorderStyle orig = new WidgetBorderStyle(
						objectView.getElement().getStyle().getBorderStyle(),
						objectView.getElement().getStyle().getBorderWidth(),
						objectView.getElement().getStyle().getBorderColor()
				);
				originalBorderStyle.put(objectView, orig);

				// apply new border style
				objectView.getElement().getStyle().setBorderStyle(this.borderStyle);
				objectView.getElement().getStyle().setBorderWidth(this.borderWidth, Unit.PX);
				objectView.getElement().getStyle().setBorderColor(this.borderColor);
			}
		}
	}

	@Override
	public void undecorateObject(T object, Widget objectView) {
		if(objectView != null && objectView.getElement() != null){
			if(originalBorderStyle.get(objectView) != null){
				WidgetBorderStyle orig = originalBorderStyle.get(objectView);
				objectView.getElement().getStyle().setProperty("borderStyle", orig.getBorderStyle());
				objectView.getElement().getStyle().setProperty("borderWidth", orig.getBorderWidth());
				objectView.getElement().getStyle().setProperty("borderColor", orig.getBorderColor());

				originalBorderStyle.remove(objectView);
			}
			else{
				// widget was not highlighted
			}
		}
	}
}
